package cn.lyd.org.controller;

import cn.lyd.base.annotation.PreAuthorize;
import cn.lyd.base.jwt.UserInfo;
import cn.lyd.base.until.AjaxResult;
import cn.lyd.base.until.PageList;
import cn.lyd.base.until.ThreadLocalUtil;
import cn.lyd.org.domain.Employee;
import cn.lyd.org.query.EmployeeQuery;
import cn.lyd.org.service.IEmployeeService;
import cn.lyd.user.domain.Logininfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "员工的API",description="员工相关的CRUD功能")
@RestController //@controller +@ResponseBody 只返回json数据，不经过视图解析器，否则不能用
@RequestMapping("/emp")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    /*restful接口*/

    /**
     * 查询所有
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询全部" )
    @PreAuthorize(name = "查询所有员工",sn = "employee:findAll")
    public List<Employee> findAll(){
        return employeeService.findAll();
    }

    /**
     * 查询一个
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    @PreAuthorize(name = "查询单个员工",sn = "employee:findById")
    public Employee findById(@PathVariable("id") Long id){
        return employeeService.findById(id);
    }

    /**
     * 添加或修改
     * @param employee
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或修改" )
    @PreAuthorize(name = "添加或修改员工",sn = "employee:addOrUpdate")
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            //判断有无id--判断是添加还是修改
            if (employee.getId()==null){
                employeeService.add(employee);
            }else {
                employeeService.update(employee);
            }
            //操作成功调用AjaxResult的无参构造
            return new AjaxResult();
        } catch (Exception e) {//操作失败
            e.printStackTrace();
            //操作失败调用AjaxResult的有参构造
            return new AjaxResult(false,"操作失败");
        }

    }


    /**
     * 分页查询
     * @param employeeQuery
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询" )
    @PreAuthorize(name = "分页查询员工",sn = "employee:queryPage")
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery employeeQuery){
        //获取当前登录人信息+权限+菜单
        UserInfo userInfo = ThreadLocalUtil.getUserInfo();
        //获取当前登录人的个人信息
        Logininfo logininfo = userInfo.getLogininfo();
        //获取当前登录人的shop，判断 非空=店长 -- 空=平台
        if(logininfo.getShop()!=null){//店长
            Long shop_id = logininfo.getShop().getId();
            //shop_id传到sql
            employeeQuery.setShop_id(shop_id);
        }
        return   employeeService.queryByPage(employeeQuery);
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除" )
    @PreAuthorize(name = "删除一个员工",sn = "employee:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            //如果删除成功，调用AjaxResult的无参构造
            employeeService.delete(id);
            return  new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }


    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping()
    @ApiOperation(value = "批量删除对象" )
    @PreAuthorize(name = "批量删除员工",sn = "employee:patchDel")
    public AjaxResult patchDel(@RequestBody Long[] ids){
        try {
            //如果删除成功，调用AjaxResult的无参构造
            employeeService.batchDelete(ids);
            return  new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }



    /**
     * 查询某个店铺的员工
     * @param id 店铺id
     * @return 某个店铺的所有员工
     */
    @GetMapping("/list/{id}")
//    @ApiOperation(value = "通过ShopId查询" )
//    @PreAuthorize(name = "查询某个店铺的员工",sn = "employee:findByShopId")
    public List<Employee> findByShopId(@PathVariable("id") Long id){
        return employeeService.findByShopId(id);
    }


}
